
<%@page import="java.net.URLEncoder"%>
<%@page import="com.spmanage.util.HttpUtil"%>
<%@page import="com.spmanage.util.DateUtil"%>
<%@page import="com.spmanage.util.BillingUtil"%>
<%@page import="com.spmanage.util.DbOperation"%>
<%@page import="com.spmanage.util.SqlUtil"%><%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

<%@page import="java.io.*"%>
<%@page import="org.jdom.input.SAXBuilder,org.jdom.Document"%>
<%@page import="org.jdom.Element"%>
<%@page import="org.w3c.dom.NodeList"%>
<%
    System.out.println("滚石接口-报告同步");
	String syncDataSourceIp = request.getRemoteHost();
	String recvMsgType = "";
	String saveGwid = "";
	response.setContentType("text/plain");
	PrintWriter out1 = response.getWriter();

	InputStream in = request.getInputStream();
	StringBuffer provisionReq = new StringBuffer(512);
	byte[] buf = new byte[1024];
	int len = 0;
	try {
		while ((len = in.read(buf)) != -1) {
			provisionReq.append(new String(buf, 0, len, "gb2312"));
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
	in = new ByteArrayInputStream(provisionReq.toString().trim().getBytes("gb2312"));
	SAXBuilder builder = new SAXBuilder();
	try {
		Document doc = builder.build(in);
		Element root = doc.getRootElement();
		String rootElementName = root.getName();
		
		//解析节点
		String feeType = root.getChild("DATA_TYPE").getValue();//CO_SMS_REPORT
		String linkId = root.getChild("TRANSACTIONID").getValue();
		//String spNo = root.getChild("SRC_TERMID").getValue();
		String userPhone = root.getChild("DST_TERMID").getValue();
		String momsg = root.getChild("CONTENT").getValue();           //下行内容  
		String srvCode = root.getChild("SERVICEID").getValue();
		String fee = root.getChild("FEE_CODE").getValue();//资费
		String status = root.getChild("STATUS").getValue();//成功为：DELIVRD；其他为错误码
		String date=root.getChild("SENDTIME").getValue();
		
		String org = SqlUtil.getStringResult("select org from "+BillingUtil.getSendDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);
	    if(org!=null&&org.length()>0) {
	    	System.out.println("该linkid已经有同步状态");
	    	return;
	    }
		
		
		String spNo = SqlUtil.getStringResult("select dest from "+BillingUtil.getRecvDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);
    	String content = SqlUtil.getStringResult("select body from "+BillingUtil.getRecvDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);
    	
    	//指令处理,返回处理后的指令
    	String strMo = BillingUtil.getMo(spNo, content);
    	int channel = BillingUtil.getUserChannel(spNo,strMo);
    	int spid = BillingUtil.getUserSpId(spNo,strMo);
    	int ischeck = BillingUtil.getCheckSend(linkId);
    	
    	int provinceId = BillingUtil.getProvinceId(userPhone);
    	System.out.println(provinceId);
		StringBuilder sb = new StringBuilder();
	    sb.append("'");
	    sb.append(spid);
	    sb.append("',");
	    sb.append("1");//msgmode
	    sb.append(",");
	    sb.append("15");//fmt
	    sb.append(",");
	    sb.append("0");//futype
	    sb.append(",");
	    sb.append("2");//ftype
	    sb.append(",");
	    sb.append(fee);//feecode  
	    sb.append(",");
	    sb.append("'");
	    sb.append("XXLYYY");//stype
	    sb.append("',");
	    sb.append("1");//num
	    sb.append(",");
	    sb.append("'");
	    sb.append(userPhone);
	    sb.append("',");
	    sb.append("'");
	    sb.append(spNo);
	    sb.append("',");
	    sb.append("'");
	    sb.append(userPhone);
	    sb.append("',");
	    sb.append("getDate()");  //tm
	    sb.append(",");
	    sb.append("'");
	    sb.append("mid");  //mid
	    sb.append("',");
	    sb.append("'");
	    sb.append("msgid");  //msgid
	    sb.append("',");
	    sb.append("0");//result  DELIVRD
	    sb.append(",");
	    sb.append("'");
	    sb.append(status);//DELIVRD
	    sb.append("',");
	    sb.append("'");
	    sb.append(channel);//channel
	    sb.append("',");
	    sb.append("'");
	    sb.append(linkId);//linkid
	    sb.append("','"+content+"'");
	    sb.append(",");
	    sb.append(provinceId);
	    sb.append(",");
	    sb.append(ischeck);
	    
	    
	    SqlUtil.executeUpdate("insert into "+BillingUtil.getSendDay()+"(areaid,msgMode,fmt,futype,ftype,feecode,stype,num,org,dest,fee,tm,mid,msgid,result,stat,infoid,linkid,body,province,ischeck) values("+sb.toString()+")",DbOperation.DB_STAT);
	    date = DateUtil.nowTimeTransformCharacter(DateUtil.getNow_Timestamp().toString());//200912121200
    	System.out.println("滚石端口:"+spNo+";指令："+content+";spid:"+spid+",channel:"+channel+"");
    	System.out.println("滚石接口-报告是否需要扣量："+ischeck+",linkid:"+linkId);
	    if(ischeck == 0) {
	    	System.out.println("滚石-状态同步:"+SqlUtil.getStringResult("select cpname from adminuser where id = "+ channel, 5));
	    	String mtUrl = BillingUtil.getMtUrlByChannel(channel);
    		//如果cp地址中含有'?'+参数的，就用'&'
	    	if(mtUrl.indexOf("?")>0){
    			mtUrl = mtUrl+"&";
    		}else{    			
	    		mtUrl = mtUrl+"?";
    		}
    		mtUrl = mtUrl+"spNo="+spNo+"&userPhone="+userPhone+"&linkId="+linkId+"&fee="+fee+"&status="+status+"&date="+date+"&mo="+content+"";
		    System.out.println("滚石-用户渠道号:"+channel+";用户报告地址："+mtUrl);
			HttpUtil.getHtmlSource(mtUrl,"UTF-8");

	    }
		String xmlStr="<?xml version=\"1.0\" encoding=\"gb2312\" ?><MASP_RESP><RESULT>0</RESULT><RESULT_MSG>OK</RESULT_MSG></MASP_RESP>";
		out1.print(xmlStr);
		out1.flush();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			out1.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
%>
